<?php
//商品信息
class Controller_Admin_Goods extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'goods';
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shopId', '=', $this->_shopId);
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加商品
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改商品信息
	//$nid int 商品id
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;


            // 下发消息
            if (! empty($post) )
            {
                //$goods = new App_Goods($nid);
                //$goods->pushAppMessage();
            }
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['price'] = number_format($this->_form_data['price'] / 100, 2);
            $this->_form_data['shaiMoney'] = number_format($this->_form_data['shaiMoney'] / 100, 2);
            $this->_form_data['origPrice'] = number_format($this->_form_data['origPrice'] / 100, 2);
            $this->_form_data['tuanPrice'] = number_format($this->_form_data['tuanPrice'] / 100, 2);
            $this->_form_data['thumb'] = explode('|', $data['thumb']);
            $this->_form_data['images'] = explode('|', $data['images']);
        }


        parent::action_update($nid);

    }

	//商品详细
	//$nid int 商品id
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['price'] = '￥'. number_format($this->_form_data['price'] / 100, 2);
        $this->_form_data['shaiMoney'] = '￥' .number_format($this->_form_data['shaiMoney'] / 100, 2);
        $this->_form_data['origPrice'] = '￥'. number_format($this->_form_data['origPrice'] / 100, 2);
        $this->_form_data['tuanPrice'] = '￥'. number_format($this->_form_data['tuanPrice'] / 100, 2);
        $this->_form_data['thumb'] = explode('|', $data['thumb']);
        $this->_form_data['images'] = explode('|', $data['images']);

        parent::action_detail($nid);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        foreach ($arr_config as & $rs)
        {
            if ($rs['input_name'] == 'name')
            {
                $rs['class'] = 'line';
                $rs['input_type'] = 'text';
            }
        }
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        $post['price'] = (int) ($post['price'] * 100);
        $post['shaiMoney'] = (int) ($post['shaiMoney'] * 100);
        $post['origPrice'] = (int) ($post['origPrice'] * 100);
        $post['tuanPrice'] = (int) ($post['tuanPrice'] * 100);
        $post['tuanPersons'] = (int) $post['tuanPersons'];
        $post['uid']   = user::current_user_id();
        $post['thumb'] = implode('|', $post['thumb']);
        $post['images'] = implode('|', $post['images']);
    }

	//数据格式改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            $rs['price']  = '￥'. number_format($rs['price'] / 100, 2);
            $rs['shopId'] = App_Shop::name($rs['shopId']);
        }
    }

	//属性菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        $menus[] = '<a href="/admin/goodsattr/goods?goodsId='.$row['id'].'">属性</a>';
    }

}
